{% extends "package/base.html" %}

{% load crispy_forms_tags i18n page_metadata_tags %}

{% block metadata %}
    {% translate "package documentation form" as trans_title %}
    {% page_metadata page_title=trans_title %}
{% endblock metadata %}

{% block body %}

    <h2><a href="{% url 'home' %}">{% translate "home" %}</a> / <a href="{% url 'packages' %}">{% translate "packages" %}</a> / {{ action|title }} {% translate "package" %}</h2>

    <p id="package-form-message"></p>
    <form action="{{ request.path }}" method="post" id="package-form">
        {{ form.errors }}
        {% csrf_token %}
        <fieldset>
            {{ form|crispy }}
        </fieldset>

        <input class="btn btn-default" type="submit" name="add" value="{{ action }} {% translate "Submit" %}">
    </form>

{% endblock body %}

{% block extra_body %}
    {% if not package.documentation_url.strip %}
        <script type="text/javascript">
            $(function() {
                var url = "https://readthedocs.org/api/v1/build/{{ package.slug }}/?format=jsonp";
                $.ajax({
                    url: url,
                    dataType: 'jsonp',
                    success:function(data){
                        if (data.objects.length > 0){
                            var href = "https://readthedocs.org/projects/{{ package.slug }}";
                            $("#id_documentation_url").val(href);
                        };
                    }
                });
            });
        </script>
    {% endif %}

{% endblock extra_body %}
